#include "qpointfsproperty.h"


#include "../xmlobject.h"

#include "../qpropertyfactory.h"
#include "../qcommonstruct.h"

#include <QPainter>
#include <QGradient>
#include <QLinearGradient>

QPointFsProperty::QPointFsProperty(QAbstractProperty *parent):
    QAbstractProperty(parent)
{
    set_property("type","PointFs");
    QPointFs ps;
    ps.append(QPointF(0.5,0));
    ps.append(QPointF(0,1));
    ps.append(QPointF(1,1));
    m_value.setValue<QPointFs>(ps);
}

QString QPointFsProperty::get_value_text()
{
    QPointFs ps=get_value().value<QPointFs>();
    if(ps.size()==0)
    {
        return tr("None");
    }
    else
    {
        return QString(tr("Point[%1]")).arg(ps.size());
    }
}

QIcon QPointFsProperty::get_value_icon()
{
    return QIcon();
}

void QPointFsProperty::toObject(XMLObject *xml)
{
    if(xml!=NULL)
    {
        xml->clear();
        xml->set_title(m_propertys.value("name").toString());

        QMap<QString,QVariant> pros=m_propertys;
        pros.remove("name");
        pros.remove("value");
        QMapIterator<QString,QVariant>      it(pros);

        while(it.hasNext())
        {
            it.next();
            xml->set_property(it.key(),it.value().toString());
        }

        XMLObject *obj;
        QPointFs ps=get_value().value<QPointFs>();

        foreach(QPointF pt,ps)
        {
            obj=new XMLObject(xml);
            obj->set_title("Point");
            obj->set_property("X",QString::number(pt.x()));
            obj->set_property("Y",QString::number(pt.y()));
        }
    }
}

void QPointFsProperty::fromObject(XMLObject *xml)
{
    if(xml!=NULL)
    {
        QMapIterator<QString,QString>      it(xml->get_propertys());

        while(it.hasNext())
        {
            it.next();
            m_propertys.insert(it.key(),it.value());
        }

        QPointFs ps;
        foreach(XMLObject* obj,xml->get_children())
        {
            QPointF pt;
            pt.setX(obj->get_property("X").toDouble());
            pt.setY(obj->get_property("Y").toDouble());
            ps.append(pt);
        }
        m_value.setValue<QPointFs>(ps);
        refresh();
    }
}

QString QPointFsProperty::get_property_name()
{
    return "PointFs";
}

bool QPointFsProperty::cmp(const QVariant &value)
{
    QPointFs ps1=get_value().value<QPointFs>();
    QPointFs ps2=value.value<QPointFs>();
    return ps1==ps2;
}

qRegisterProperty(QPointFsProperty)
